package com.ka.magicexplorer.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public abstract class DatabaseStorage<T> extends SQLiteOpenHelper {

	protected static String dbname = "magicexdb";
	protected String table_name;
	
	protected String primary_name 	= "path";
	protected String col_name 		= "name";
	protected String col_isHiden 	= "ishiden";

	public DatabaseStorage(Context context) {
		super(context, dbname, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	// insert
	public void addObject(T contact) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = attachData2Values(contact);

		db.insert(table_name, null, values);
		db.close();
	}

	// get data
	public T getObject(String id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(table_name, getInfoOfTable(), primary_name
				+ "=?", new String[] { id }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();

		T object = getDataFromCursor(cursor);

		db.close();
		return object;
	}

	// get all data
	public List<T> getAllObject() {
		List<T> objectList = new ArrayList<T>();
		String selectQuery = "SELECT  * FROM " + table_name;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				T object = getDataFromCursor(cursor);
				objectList.add(object);
			} while (cursor.moveToNext());
		}

		db.close();
		return objectList;
	}

	// update data
	public int updateObject(T obj) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = attachData2Values(obj);
		int result =  db.update(table_name, values, primary_name + " = ?",
				new String[] { getIDFromObject(obj) });
		
		db.close();
		return result;
	}
	
	// delete data
    public void deleteObject(T obj) {
        SQLiteDatabase db = this.getWritableDatabase();
        
        db.delete(table_name, primary_name + " = ?",
                new String[] { getIDFromObject(obj) });
        db.close();
    }
    
    // get data count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + table_name;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
 
        db.close();
        return cursor.getCount();
    }

	// 4 getAllObject, getObject
	public T getDataFromCursor(Cursor cursor) {
		return null;
	}

	
	// 4 addObject
	public ContentValues attachData2Values(T obj) {
		return null;
	}

	// 4 getObject
	public String[] getInfoOfTable() {
		return null;
	}
	
	// 4 deleteObject, updateObject
	public String getIDFromObject(T obj){
		return null;
	}

}
